import { ElMessage } from 'element-plus';
import { elMessageType, storageType } from '../model/enum';
import { getMusicUrl } from '../api/api';
import { Song } from '../model';
import useThemeStore from '../store/theme';
import { Ref } from 'vue';
import { storeToRefs } from 'pinia';

//创建一个elmessage的提示
export const elMessage = (type: string = 'success', msg: string): void => {
  switch (type) {
    case elMessageType.SUCCESS:
      ElMessage.success(msg);
      break;
    case elMessageType.ERROR:
      ElMessage.error(msg);
      break;
    case elMessageType.INFO:
      ElMessage.info(msg);
      break;
    case elMessageType.WARNING:
      ElMessage.warning(msg);
      break;
  }
};

//设置浏览器本地存储
export const setStorAge = (type: string, key: string, value: any): void => {
  switch (type) {
    case storageType.LOCAL:
      {
        if (typeof value == 'object') {
          localStorage.setItem(key, JSON.stringify(value));
        } else {
          localStorage.setItem(key, value);
        }
      }

      break;
    case storageType.SESSION:
      {
        if (typeof value == 'object') {
          sessionStorage.setItem(key, JSON.stringify(value));
        } else {
          sessionStorage.setItem(key, value);
        }
      }
      break;
  }
};

//获取浏览器本地存储
export const getStorage = (type: string, key: string): any => {
  switch (type) {
    case storageType.LOCAL: {
      try {
        return JSON.parse(localStorage.getItem(key) as string);
      } catch {
        return localStorage.getItem(key);
      }
    }
    case storageType.SESSION: {
      try {
        return JSON.parse(sessionStorage.getItem(key) as string);
      } catch {
        return sessionStorage.getItem(key);
      }
    }
  }
};

//删除浏览器本地存储
export const removeStorage = (type: string, key: string): void => {
  switch (type) {
    case storageType.LOCAL:
      {
        if (localStorage.getItem(key)) {
          localStorage.removeItem(key);
        }
      }
      break;
    case storageType.SESSION:
      {
        if (sessionStorage.getItem(key)) {
          sessionStorage.removeItem(key);
        }
      }
      break;
  }
};

//获取当前时间的时间戳
export const getTimeStamp = (): number => {
  return new Date().getTime();
};

//转换时间，将毫秒的时间转为00:00类型的时间
export const transformTime = (time: string): string => {
  const temp: number = Math.floor(+time / 1000);
  const seconds: string = temp % 60 < 10 ? '0' + (temp % 60) : '' + (temp % 60);
  const minutes: string =
    temp / 60 < 10 ? '0' + Math.floor(temp / 60) : '' + Math.floor(temp / 60);
  return minutes + ':' + seconds;
};

//将字符串毫秒时间进行转化为妙
export const transformTotalTime = (time: string): number => {
  return +time / 1000;
};

//将时间戳格式化为year-month-day格式
export const formatTime = (time: string): string => {
  const date: Date = new Date(time);
  return date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();
};

//获取音乐的url
//ids是通过，分割的多个id,list是放歌曲的容器,key字段判断要不要做持久化
export const getMusicUrls = (ids: string, list: Song[], key?: string) => {
  getMusicUrl(ids)
    .then((response: any) => {
      const { data } = response;
      data.forEach((item: any) => {
        const index = list.findIndex((song) => song.id == item.id);
        if (item.url) {
          list[index].url = item.url;
          list[index].time = item.time;
        } else {
          list[index].available = '10';
          list[index].url = '';
          list[index].time = '0';
        }
      });
      //判断key要不要做持久化
      if (key) {
        setStorAge(storageType.SESSION, key, list);
      }
    })
    .catch((err: any) => {
      elMessage(elMessageType.ERROR, err.message);
    });
};

//从返回数据中获取音乐信息,并放入容器
export const getMusicInfos = (
  ids: string[],
  list: Song[],
  data: any,
  love?: boolean
) => {
  const { id, name, ar, al, fee } = data;
  ids.push(id);
  list.push({
    id: id,
    name: name,
    singer: ar.map((item: any) => item.name).join('、'),
    songImage: al.picUrl,
    album: al.name,
    available: fee,
    isLove: love ? true : false,
    url: '',
    time: '',
  });
};

//获取主题
export const getTheme = (): Map<string, Ref<string>> => {
  const themes = useThemeStore();
  const {
    fontBlack,
    fontColor,
    searchBg,
    background,
    menuColor,
    tableHover,
    shadow,
    active,
  } = storeToRefs(themes);
  const themeMap: Map<string, Ref<string>> = new Map();
  themeMap.set('fontBlack', fontBlack);
  themeMap.set('fontColor', fontColor);
  themeMap.set('searchBg', searchBg);
  themeMap.set('background', background);
  themeMap.set('menuColor', menuColor);
  themeMap.set('tableHover', tableHover);
  themeMap.set('shadow', shadow);
  themeMap.set('active', active);
  return themeMap;
};

export const handleSingerName = (name: string): string => {
  return name.includes('、') ? name.split('、')[0] : name;
};

//将时间转换为时间戳
export const formatToTimeStap = (time: string): number => {
  if (time.includes('.')) {
    const min = time.split(':')[0];
    const ms = time.split(':')[1];
    return +min * 60 * 1000 + +ms * 1000;
  } else {
    const min = time.split(':')[0];
    const s = time.split(':')[1].split('.')[0];
    return +min * 60 * 1000 + +s * 1000;
  }
};
